<template>
  <div id='entrepot_edit' class="Row_content">
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="110">
      <FormItem label="仓库名称：" prop="potName">
        <Input v-model="formValidate.potName" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='10'></Input>
      </FormItem>
      <FormItem label="仓库缩写：" prop="potInitil">
        <Input v-model="formValidate.potInitil" placeholder="请输入" class='input-wid' style="width: 260px;" :maxlength='4'></Input>
      </FormItem>
      <FormItem label="所属部门/门店：" prop="organization">
        <Input v-model="formValidate.organization" placeholder="请输入" class='input-wid' style="width: 260px;" disabled></Input>
      </FormItem>
      <FormItem label="是否显示：" prop='isShow'>
        <RadioGroup v-model="formValidate.isShow">
          <Radio label="1" >显示</Radio>
          <Radio label="2" style="margin-left: 25px">隐藏</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="仓库简介：" style="width: 450px" prop='remark'>
        <Input v-model="formValidate.remark" :maxlength='30'
          type="textarea" style="width:320px"
          :autosize="{ minRows: 3, maxRows: 5 }"
          placeholder="请输入内容"></Input>
      </FormItem>
      <FormItem>
        <Button type="primary" class="btn" @click="handleSubmit">确定</Button>
        <Button class="btn" style="margin-left: 24px" @click="handleReset">取消</Button>
      </FormItem>
    </Form>
  </div>
</template>

<script>
import { entrepotEdit, entrepotDetail } from '@/api/purchase/index'
import { routeEqual } from '@/libs/util'
import { mapState } from 'vuex'
export default {
  name: 'entrepot_edit',
  computed: {
    ...mapState({
      merchantId: state => state.user.merchantId,
      userId: state => state.user.userId,
      userName: state => state.user.userName,
      orgGuid: state => state.user.orgId,
      orgName: state => state.user.orgName
    })
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (vm.whCode !== to.params.whCode) {
        vm.whCode = to.params.whCode
        vm.getDetail()
      }
    })
  },
  data () {
    return {
      whCode: '',
      submitLock: false,
      tagList: [],
      formValidate: {
        potName: '',
        potInitil: '',
        organization: '',
        isShow: '',
        remark: ''
      },
      ruleValidate: {
        potName: { required: true, message: '请填写仓库名称', trigger: 'blur' },
        potInitil: { required: true, message: '请填写仓库缩写', trigger: 'blur' },
        organization: { required: true, message: '请填写所属部门/门店', trigger: 'blur' },
        isShow: { required: true, message: '请选择是否显示', trigger: 'change' }
      }
    }
  },
  mounted () {
    this.whCode = this.$route.params.whCode
    this.getDetail()
  },
  methods: {
    getDetail () {
      entrepotDetail({
        merchantId: this.$store.state.user.merchantId,
        orgGuid: this.$store.state.user.orgId,
        whCode: this.whCode
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          let obj = result.data || {}
          this.formValidate = {
            potName: obj.whName,
            potInitil: obj.whAbbr,
            isShow: obj.whStatus + '',
            remark: obj.remark,
            organization: obj.orgName
          }
        } else {
          this.$Message.error(result.error || result.message)
        }
      }).catch(err => {
        this.submitLock = false
        this.$Message.error(`${err}`)
      })
    },
    handleSubmit () {
      this.$refs.formValidate.validate(valid => {
        if (valid) {
          if (this.submitLock) {
            return
          }
          this.submitLock = true
          entrepotEdit({
            merchantId: this.merchantId,
            orgGuid: this.orgGuid,
            whCode: this.whCode,
            operId: this.userId,
            operName: this.userName,
            whName: this.formValidate.potName, // 仓库名称
            whAbbr: this.formValidate.potInitil, // 仓库缩写
            remark: this.formValidate.remark,
            showFlag: this.formValidate.isShow
          }).then(res => {
            this.submitLock = false
            if (res.data.status === 200) {
              this.$Message.success('编辑成功！')
              this.handleReset()
            } else {
              this.$Message.error(res.data.error || res.data.message)
            }
          })
        }
      })
    },
    handleReset () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'entrepot_list'
        })
      })
    }
  }
}
</script>
